package com.glasses.business.fragment;

import android.os.Bundle;
import android.view.View;

import com.glasses.business.adapter.MessageAdapter;
import com.glasses.business.base.BaseListFragment;
import com.glasses.business.base.MyBaseAdapter;
import com.glasses.business.entity.MsgList;
import com.glasses.business.entity.NewsList;
import com.glasses.business.http.NetUtils;
import com.glasses.business.utils.Tools;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.ToastUtil;

import java.util.List;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/7
 * Version：1.0
 */

public class MsgListFragment extends BaseListFragment {
    private List<MsgList> list;
    @Override
    protected boolean isHaveHead() {
        return true;
    }

    @Override
    protected void setHeadViews() {
        setTitleTv("消息");
    }

    @Override
    protected MyBaseAdapter getAdapter() {
        MessageAdapter messageAdapter = new MessageAdapter(getActivity(), null);
        messageAdapter.setOnItemClickListener(new MyBaseAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, List data, int position) {
                MsgList msgList = (MsgList) data.get(position);
                Bundle bundle = new Bundle();
                bundle.putInt("id",msgList.getId());
                open(new MsgDetailFragment(), bundle);
            }
        });
        return messageAdapter;
    }

    @Override
    protected int getLineNum() {
        return 0;
    }

    @Override
    public void initData() {
        pageNum=0;
        loadDatas(true);
    }

    @Override
    public boolean mainTabShow() {
        return false;
    }

    @Override
    public void onRefresh() {
        pageNum=0;
        loadDatas(true);
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        loadDatas(false);
    }

    @Override
    public void onRetryClick() {
        pageNum=0;
        loadDatas(true);
    }

    private void loadDatas(final boolean isRefush) {
        Tools.showDialog(getActivity());
        NetUtils.getInstance().msgList(pageNum, PAEG_SIZE, new NetCallBack() {
            @Override
            public void onSuccess(String response, String mssage, ResultModel model) {
                Tools.dismissWaitDialog();
                mRecyclerview.loadMoreComplete();
                mRecyclerview.refreshComplete();
                mRefeshLy.hideAll();
                list = (List<MsgList>) model.getModelList();
                if (isRefush) {
                    adapter.clear();
                }
                adapter.append(list);
                if (list == null || list.size() < 10) {
                    if (isRefush && (list == null || list.size() == 0)) {
//                        mRefeshLy.showEmptyView();
                        mRefeshLy.showFailView();
                    }
                    mRecyclerview.setLoadingMoreEnabled(false);
                } else {
                    mRecyclerview.setLoadingMoreEnabled(true);
                }
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
                //                mRefeshLy.hideAll();
                mRefeshLy.showFailView();
                mRecyclerview.loadMoreComplete();
                mRecyclerview.refreshComplete();
            }
        }, MsgList.class);
    }
}
